from _socket import inet_aton
from _struct import unpack
import re, time

def ip2long(ip_addr):
    ip_packed = inet_aton(ip_addr)
    ip = unpack("!L", ip_packed)[0]

    return ip

def cross_detection(range):
    list = []
    for item in range:
        list.append((item['begin'], item['end']))

    list.sort()

    prev = None
    for item in list:
        if prev is not None:
            if prev[1] >= item[0] or item[0] > item[1]:
                return True

        prev = item

    return False

def run():
    fp = open('ip.txt', 'rb')

    ip_parts = []
    pattern = re.compile(r'^([.\d]+)\s+([.\d]+)\s+(.+)$')

    for line in fp:
        m = pattern.match(line.replace('CZ88.NET', '').strip())
        if m:
            ip_parts.append({
                'begin': ip2long(m.group(1)),
                'end': ip2long(m.group(2)),
                'memo': m.group(3)
            })

    print cross_detection(ip_parts)


if __name__ == '__main__':
    run()
